/*
 * @Descripttion:
 * @version:
 * @Author: hutao
 * @Date: 2023-04-26 10:25:55
 * @LastEditors: hutao
 * @LastEditTime: 2023-06-19 17:38:38
 */
import { Column } from "htui-yllkbz/src/packages/type";
import cronstrue from "cronstrue/i18n";
export function cronString(val: string) {
  if (val)
    return cronstrue.toString(val, {
      locale: "zh_CN",
      dayOfWeekStartIndexZero: false,
    });
  else {
    return "--";
  }
}
/** 产品列表
 * @params hide 是否隐藏最后三个列表
 */
export function productColumn(hide = false): Column[] {
  return [
    { key: "product.name", title: "产品名称" },
    { key: "product.productCode", title: "产品编码" },
    { key: "product.specification", title: "规格型号" },

    { key: "productCategory.name", title: "产品类别" },
    { key: "productFactory.factoryName", title: "厂家", sortable: true },
    { key: "supplier.supplierName", title: "供应商", sortable: true },
    { key: "product.price", title: "参考价", sortable: true },
    { key: "product.warranty", title: "保质期", sortable: true },
    {
      key: "product.minStock",
      title: "安全库存数量",
      sortable: true,
      width: 120,
    },
    {
      key: "product.currentStock",
      title: "当前库存数量",
      sortable: true,
      width: 120,
    },

    { key: "isMustReturned", title: "归还要求", hide: hide },
    { key: "handel", title: "操作", hide: hide },
  ];
}
export function InventoryColumn(readonly?: boolean): Column[] {
  return [
    { key: "name", title: "仓库名称" },
    { key: "code", title: "库房编码" },
    { key: "description", title: "库房描述" },
    {
      key: "position",
      title: "仓库位置",
      type: "common",
      commonType: "baseDataId",
    },
    { key: "organizationUnitId", title: "所属部门", type: "org" },
    { key: "administrators", title: "管理员" },
    { key: "handel", title: "操作", width: 120, hide: !!readonly },
  ];
}
/** 盘点计划 */
export function stoclColumn(hide = false): Column[] {
  return [
    { key: "name", title: "名称" },
    { key: "planType", title: "类别" },
    {
      key: "cron",
      title: "循环规则",
    },
    { key: "creationTime", title: "创建时间", type: "time", width: 130 },
    {
      key: "creatorId",
      title: "创建人",
      type: "userId",
      width: 90,
      align: "center",
    },
    { key: "status", title: "状态" },
    // { key: "下次任务", title: "下次任务" },

    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 盘点任务 */
export function stoclTaskColumn(hide = false): Column[] {
  return [
    { key: "title", title: "名称" },
    { key: "plan.name", title: "来源计划" },
    {
      key: "dateBegin",
      title: "计划开始时间",
      type: "date",
    },
    { key: "status", title: "状态" },
    { key: "dateEnd", title: "计划结束时间", type: "date" },
    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 盘点单 */
export function stoclFormColumn(hide = false): Column[] {
  return [
    { key: "title", title: "名称" },
    { key: "batch", title: "编号" },
    { key: "plan.name", title: "来源计划" },
    { key: "job.title", title: "来源任务" },
    {
      key: "creationTime",
      title: "盘点时间",
      type: "time",
    },
    { key: "status", title: "状态" },

    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 采购视图 */
export function pruchaseColumn(hide = false): Column[] {
  return [
    { key: "name", title: "名称" },
    { key: "编号", title: "编号" },
    { key: "specification", title: "规格型号" },
    {
      key: "cron",
      title: "计划领取时间",
      type: "date",
    },
    { key: "creationTime", title: "申请部门", type: "org" },
    { key: "creatorId", title: "需采购数量" },
    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 采购单据视图 */
export function pruchaseDocColumn(hide = false): Column[] {
  return [
    { key: "title", title: "名称" },
    { key: "code", title: "单号" },
    {
      key: "planUseTime",
      title: "计划领取时间",
      type: "date",
      sortable: true,
    },
    { key: "requestOrganizationUnitName", title: "申请部门", sortable: true },
    { key: "state", title: "状态", sortable: true },
    {
      key: "creationTime",
      title: "创建时间",
      type: "time",
      width: 120,
      sortable: true,
    },
    {
      key: "creatorId",
      title: "创建人",
      type: "userId",
      sortable: true,
      width: 90,
      align: "center",
    },
    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 采购备件列表 */
export function pruchaseApplyColumn(
  hide = false,
  completedNum = false
): Column[] {
  return [
    { key: "product.name", title: "名称" },
    { key: "product.productCode", title: "编号" },
    { key: "product.specification", title: "规格型号" },
    { key: "currentStock", title: "库存数量" },
    { key: "price", title: "单价" },
    { key: "num", title: "采购数量", showOverflowTooltip: false },
    {
      key: "completedNum",
      title: "已采购数量",
      showOverflowTooltip: false,
      hide: completedNum,
    },
    { key: "remark", title: "备注", hide: hide, showOverflowTooltip: false },
    { key: "allPrice", title: "总价" },
    { key: "handel", title: "操作", hide: hide },
  ];
}

/** 申领列表 */
export function applyListColumn(hide = false): Column[] {
  return [
    { key: "title", title: "名称" },
    { key: "code", title: "单号" },

    {
      key: "requestUserId",
      title: "申请人",
      type: "userId",
      sortable: true,
    },
    { key: "requestOrganizationUnitName", title: "申请部门" },

    { key: "state", title: "状态", sortable: true },
    {
      key: "planUseTime",
      title: "计划领取时间",
      type: "date",
      sortable: true,
    },
    {
      key: "businessType",
      title: "来源业务",
    },
    {
      key: "businessName",
      title: "来源单据",
    },
    // {
    //   key: "来源单号",
    //   title: "来源单号",
    // },
    {
      key: "creationTime",
      title: "创建时间",
      type: "time",
      sortable: true,
      width: 120,
    },
    {
      key: "creatorId",
      title: "创建人",
      type: "userId",
      sortable: true,
      width: 90,
      align: "center",
    },
    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 申领备件列表 */
export function applyColumn(hide = false): Column[] {
  return [
    { key: "product.name", title: "名称" },
    { key: "product.productCode", title: "编号" },
    { key: "product.specification", title: "规格型号" },
    { key: "currentStock", title: "库存数量" },
    // { key: "localStock", title: "锁定数量" },
    // { key: "validStock", title: "可使用数量" },
    { key: "isMustReturned", title: "归还需求" },
    { key: "total", title: "申领总数" },
    { key: "handel", title: "操作" },
  ];
}
/** 移库列表 */
export function moveColumn(hide = false): Column[] {
  return [
    { key: "title", title: "名称" },
    { key: "code", title: "单号" },
    { key: "requestDate", title: "申请时间", type: "time", sortable: true },
    {
      key: "requestUserId",
      title: "申请人",
      sortable: true,
      type: "common",
      commonType: "userId",
    },

    { key: "state", title: "状态", sortable: true },
    { key: "remark", title: "备注" },
    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 移库备件列表 */
export function moveProduceColumn(hide = false): Column[] {
  return [
    { key: "product.name", title: "名称" },
    { key: "product.productCode", title: "编号" },
    { key: "product.specification", title: "规格型号" },
    { key: "isMustReturned", title: "归还需求" },
    { key: "total", title: "移库总数" },
    { key: "handel", title: "操作", hide: hide },
  ];
}
/** 申领明细列表 */
export function applyDetailColumn(hide = false): Column[] {
  return [
    { key: "product.name", title: "名称" },
    { key: "product.productCode", title: "编号" },
    { key: "product.specification", title: "规格型号" },
    { key: "productFactoryId", title: "厂家" },
    { key: "supplierId", title: "供应商" },
    { key: "room.name", title: "申领仓库" },
    { key: "num", title: "申领数量" },
    { key: "completedNum", title: "已领取数量" },
    { key: "isMustReturned", title: "归还需求" },
  ];
}
/** 流程追踪 */
export function flowHistory(hide = false): Column[] {
  return [
    { key: "operation", title: "流程节点" },
    {
      key: "operator",
      title: "操作人/当前处理人",
      type: "userId",
    },
    { key: "operateTime", title: "操作时间", type: "time" },
    { key: "result", title: "操作结果" },
  ];
}
/**  获取当前用户
 * @isObj 是否返回整个对象
 *
 ** */
export function getCurrentUser(
  isObj = false
): { id?: string; signature?: string; name?: string } {
  let userInfo: any = window.localStorage.getItem("userInfo");
  if (userInfo) {
    userInfo = JSON.parse(userInfo);
    if (isObj) {
      return userInfo;
    }
    const name = userInfo.surname + userInfo.name;
    return {
      id: userInfo.id,
      signature: userInfo.signature || name,
      name: name,
    };
  }

  return {
    id: undefined,
    signature: undefined,
    name: "",
  };
}
/** 获取所有部门
 * @params userId 默认是当前用户
 */
export function getallOrg(
  userId?: string
): { id: string | undefined; displayName: string | undefined }[] {
  let userOrg: any = window.localStorage.getItem("userInOrganiza");

  if (userOrg && !userId) {
    //默认获取当前人员所在部门
    userOrg = JSON.parse(userOrg);
    return userOrg.length ? userOrg : [];
  } else if (userId) {
    //如果有人员
    const commonDatasStr: any = window.localStorage.getItem("commonDatas");
    let userList: any[] = [];
    if (commonDatasStr) {
      userList = JSON.parse(commonDatasStr).users.items;
    }
    if (userList.length) {
      const userInfo = userList.find((item) => item.id === userId);
      if (
        userInfo &&
        userInfo.organizationIds &&
        userInfo.organizationIds.length
      ) {
        const temp: any = [];
        userInfo.organizationIds.forEach((item: string, index: number) =>
          temp.push({
            id: item,
            displayName: userInfo.organizationNames[index],
          })
        );
        return temp;
      }
    }
  }
  return [];
}
/** 获取当前部门
 * @params userId 默认是当前用户
 */
export function getorg(
  userId?: string
): { id: string | undefined; displayName: string | undefined } {
  let userOrg: any = window.localStorage.getItem("userInOrganiza");

  if (userOrg && !userId) {
    //默认获取当前人员所在部门
    userOrg = JSON.parse(userOrg);
    return userOrg.length
      ? userOrg[0]
      : { id: undefined, displayName: undefined };
  } else if (userId) {
    //如果有人员
    const commonDatasStr: any = window.localStorage.getItem("commonDatas");
    let userList: any[] = [];
    if (commonDatasStr) {
      userList = JSON.parse(commonDatasStr).users.items;
    }
    if (userList.length) {
      const userInfo = userList.find((item) => item.id === userId);
      if (
        userInfo &&
        userInfo.organizationIds &&
        userInfo.organizationIds.length
      ) {
        return {
          id: userInfo.organizationIds[0],
          displayName: userInfo.organizationNames[0],
        };
      }
    }
  }
  return {
    id: undefined,
    displayName: undefined,
  };
}

/*
 *  根据某个字段实现对json数组的排序
 * @param   array  要排序的json数组对象
 * @param   field  排序字段（此参数必须为字符串）
 * @param   reverse 是否倒序（默认为false）
 */

export function jsonSort(array: any[], field: string, reverse = false) {
  // 数组长度小于2 或 没有指定排序字段 或 不是json格式数据
  if (array.length < 2 || !field || typeof array[0] !== "object") return array;
  // 数字类型排序
  if (typeof array[0][field] === "number") {
    array.sort(function(x, y) {
      return x[field] - y[field];
    });
  }
  // 字符串类型排序
  if (typeof array[0][field] === "string") {
    array.sort(function(x, y) {
      return x[field].localeCompare(y[field]);
    });
  }
  // 判断是否需要倒序
  if (reverse) {
    array.reverse();
  }
  return array;
}

export const requestStatus = (formType?: number) => {
  let state2 = "待领取";
  let state4 = "已领取";
  if (formType === 2) {
    state2 = "待移库";
    state4 = "已移库";
  }
  if (formType === 4) {
    state2 = "待采购";
    state4 = "已采购";
  }
  return [
    { title: "盘点中", key: 0, color: "#4ECC7A" },
    { title: "待审核", key: 1, color: "#E2B236" },
    { title: state2, key: 2, color: "#4ECC7A" },
    { title: "已驳回", key: 3, color: "#D54545" },
    { title: state4, key: 4, color: "#CACECB" },
    { title: "已入库", key: 5, hide: true },
    { title: "已归还", key: 6, hide: true },
    { title: "已关闭", key: 7, color: "#CACECB" },
  ];
};
